package com.allwees.bs.module.pms.vo.excel;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.opencsv.bean.CsvBindByName;
import com.allwees.bs.module.pms.entity.Sku;
import lombok.Data;
import lombok.EqualsAndHashCode;
import me.zhengjie.utils.StringUtils;

import java.math.BigDecimal;

/**
 * 一步导入Product和Sku（参考Wish模板）
 *
 * @author yangylsky
 * @since 2020-03-03
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ImportShopifyProductSkuBean extends ImportProductSkuBean {

    @ExcelProperty("Title")
    @CsvBindByName(column = "Title")
    protected String productName;

    @ExcelProperty("Body (HTML)")
    @CsvBindByName(column = "Body (HTML)")
    protected String description;

    //value = "Type",
    @ExcelIgnore
    protected String categoryName;

    @ExcelProperty("Tags")
    @CsvBindByName(column = "Tags")
    protected String keywords;

    @ExcelProperty("Option1 Name")
    @CsvBindByName(column = "Option1 Name")
    protected String option1Name;

    @ExcelProperty("Option1 Value")
    @CsvBindByName(column = "Option1 Value")
    protected String option1Value;

    @ExcelProperty("Option2 Name")
    @CsvBindByName(column = "Option2 Name")
    protected String option2Name;

    @ExcelProperty("Option2 Value")
    @CsvBindByName(column = "Option2 Value")
    protected String option2Value;

    @ExcelProperty("Option3 Name")
    @CsvBindByName(column = "Option3 Name")
    protected String option3Name;

    @ExcelProperty("Option3 Value")
    @CsvBindByName(column = "Option3 Value")
    protected String option3Value;

    @ExcelProperty("Variant SKU")
    @CsvBindByName(column = "Variant SKU")
    protected String skuCustomNo;

    @ExcelProperty("Variant Grams")
    @CsvBindByName(column = "Variant Grams")
    protected Float weight;

    @ExcelProperty("Variant Inventory Qty")
    @CsvBindByName(column = "Variant Inventory Qty")
    protected Integer quantity = 0;

    @ExcelProperty("Variant Price")
    @CsvBindByName(column = "Variant Price")
    protected BigDecimal retailPrice;

    @ExcelProperty("Variant Compare At Price")
    @CsvBindByName(column = "Variant Compare At Price")
    protected BigDecimal originalPrice;

    @ExcelProperty("Image Src")
    @CsvBindByName(column = "Image Src")
    protected String mainPhoto;

    @ExcelProperty("Variant Image")
    @CsvBindByName(column = "Variant Image")
    protected String skuImage;

    @ExcelProperty("Variant Weight Unit")
    @CsvBindByName(column = "Variant Weight Unit")
    protected String weightUnit;

    @ExcelIgnore
    protected String categoryZh;

    @ExcelIgnore
    protected String productCustomNo;

    @ExcelIgnore
    protected String color;

    @ExcelIgnore
    protected String size;

    @ExcelIgnore
    protected String currency = "USD";

    @ExcelIgnore
    protected String shippingCurrency = "USD";

    // @ExcelProperty(value = "*Shipping", index = 12)
    protected BigDecimal shippingPrice;

    // @ExcelProperty(value = "Shipping Arrival Desc", index = 13)
    protected String shippingArrivalDesc;

    @ExcelIgnore
    protected Integer shippingMinDays;

    @ExcelIgnore
    protected Integer shippingMaxDays;

    @Override
    public void initData() {
//        this.skuUuid = skuCustomNo;
        if (this.keywords != null) {
            this.keywords = this.keywords.replace(", ", ",");
        }

        if (StringUtils.equalsIgnoreCase("size", option1Name) || StringUtils.equalsIgnoreCase("尺寸", option1Name)) {
            this.size = this.option1Value;
            this.color = this.option2Value;
        } else if (StringUtils.equalsIgnoreCase("size", option2Name) || StringUtils.equalsIgnoreCase("尺寸", option2Name)) {
            this.size = this.option2Value;
            this.color = this.option1Value;
        }
    }

    @Override
    public Sku toSku() {
        Sku sku = super.toSku();
        if (StringUtils.equalsIgnoreCase("kg", sku.getWeightUnit()) && sku.getWeight() != null) {
            sku.setWeightUnit("g");
            sku.setWeight(sku.getWeight() * 1000);
        }
        if (skuImage != null) {
            sku.setPhotos(new String[]{
                    getSkuImage()
            });
        }
        return sku;
    }
}
